#include<bits/stdc++.h>
#define f first
#define pr pair
#define s second
#define ll long long
#define mp make_pair
#define pll pr<ll,ll>
#define pii pr<int,int>
#define piii pr<int,pii>
using namespace std;
ll a[200005];
ll pf[200005];
ll dp[200005];
ll me[1000006];
void sl()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=1;i<n;i++) if(a[i]==a[i-1]) pf[i]=pf[i-1]+a[i];
    else pf[i]=pf[i-1];
    ll ans=pf[n-1];
    for(int i=1;i<=1000000;i++) me[i]=-1e18;
    ll te=-1e18;
    for(int i=1;i<n;i++)
    {
        dp[i]=pf[i-1];
        dp[i]=max(dp[i],pf[i-1]+te);
        dp[i]=max(dp[i],pf[i-1]+me[a[i]]+a[i]);
        me[a[i-1]]=max(me[a[i-1]],dp[i]-pf[i]);
        te=max(te,dp[i]-pf[i]);
        ans=max(ans,pf[n-1]-pf[i]+dp[i]);
    }
    cout<<ans<<endl;
}
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios_base::sync_with_stdio(0);
    int t;
    cin>>t;
    while(t--) sl();
    return 0;
}
